<template>
  <div>
    <el-dialog :visible.sync="isShow" width="600px">
      <div slot="title" class="title">{{mode=='add'? '新增企业':'编辑企业'}}</div>
      <el-form :model="form" :rules="rules" label-width="80px" ref="form">
        <el-form-item label="企业编号" prop="eid">
          <el-input v-model="form.eid"></el-input>
        </el-form-item>
        <el-form-item label="企业名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="企业简称" prop="short_name">
          <el-input v-model="form.short_name"></el-input>
        </el-form-item>
        <el-form-item label="企业简介" prop="intro">
          <el-input v-model="form.intro"></el-input>
        </el-form-item>
        <el-form-item label="企业备注" prop="remark">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="footer">
        <el-button @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addBussiness, editBussiness } from "@/api/bussiness.js";
export default {
  data() {
    return {
      mode: "add",
      isShow: false,
      form: {
        eid: "", //	是	string	企业编号
        name: "", //	是	string	企业名称
        short_name: "", //	是	string	简称
        intro: "", //	是	string	企业简介
        remark: "", //	否	string	备注
      },
      rules: {
        eid: [{ required: true, message: "请输入企业编号", trigger: "blur" }],
        name: [{ required: true, message: "请输入企业名称", trigger: "blur" }],
        short_name: [
          { required: true, message: "请输入企业简称", trigger: "blur" },
        ],
        intro: [{ required: true, message: "请输入企业简介", trigger: "blur" }],
      },
    };
  },
  watch: {
    isShow(newVal) {
      if (newVal == false) {
        this.form = {
          eid: "", //	是	string	企业编号
          name: "", //	是	string	企业名称
          short_name: "", //	是	string	简称
          intro: "", //	是	string	企业简介
          remark: "", //	否	string	备注
        }
        this.$nextTick(()=>{
            this.$refs.form.clearValidate()
        })
      }
    },
  },
  methods: {
    submit() {
      this.$refs.form.validate((res) => {
        if (res) {
          if (this.mode == "add") {
            addBussiness(this.form).then(() => {
              this.$message.success("新增成功");
              this.$parent.search();
              this.isShow = false;
            });
          } else {
            editBussiness(this.form).then(() => {
              this.$message.success("新增成功");
              this.$parent.search();
              this.isShow = false;
            });
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.title {
  height: 53px;
  background: linear-grident(to right, #01c4fa, #1394fa);
  color: #fff;
}
</style>